home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Magazine Collection 2001
/
Delphi Magazine Collection 20001 (2001).iso
/
DISKS
/
ISSUE18
/
SURVIVE
/
DMLOGIN.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-12-03
|
4KB
|
132 lines
unit DMLogin;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DB, DBTables, Login;
const
evtLoginSuccessful = 100;
evtLoginFail = 101;
evtLogout = 102;
evtChangePassword = 103;
type
TLoginDM = class(TDataModule)
qryGetUserValues: TQuery;
qryPostAuditTrail: TQuery;
qrySetLastLoggedInDate: TQuery;
dbInternal: TDatabase;
protected
FLogin: TLoginManager;
public
constructor Create(AOwner: TComponent); override;
procedure GetUserValues(var UserID: LongInt;
var FirstName: String;
var LastName: String;
var DateLastLogin: TDateTime;
var PasswordExpired: Boolean);
procedure PostAuditTrail(EventID: Integer; EventMsg: String);
procedure PostUserLoginDate;
end;
var
LoginDM: TLoginDM;
implementation
{$R *.DFM}
{ The system account info will typically be implemented
in a more secure fashion, like in a DLL. }
const
SystemAccountUserName = 'SYSTEM';
SystemAccountPassword = 'onomatopoeia';
constructor TLoginDM.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if not (AOwner is TLoginManager) then
raise Exception.Create('TLoginDM cannot be created independently of TLoginManager.');
FLogin := AOwner as TLoginManager;
with dbInternal do
begin
Params.Values['USER NAME'] := SystemAccountUserName;
Params.Values['PASSWORD'] := SystemAccountPassword;
end;
end;
procedure TLoginDM.GetUserValues(var UserID: LongInt;
var FirstName: String;
var LastName: String;
var DateLastLogin: TDateTime;
var PasswordExpired: Boolean);
{ Returns key information about the current user }
var
DateLastChange: TDateTime;
begin
with qryGetUserValues do
begin
Close;
ParamByName('Username').AsString := FLogin.Username;
Open;
try
if Eof then
raise Exception.Create('Invalid login.');
UserID := FieldByName('UserID').AsInteger;
FirstName := FieldByName('FirstName').AsString;
LastName := FieldByName('LastName').AsString;
DateLastLogin := FieldByName('DateLastLogin').AsDateTime;
{ if DateLastPasswordChange is null, then password change forced }
if FieldByName('DateLastPasswordChange').IsNull then
PasswordExpired := True
else
{ if PasswordLifespan is null, then password never expires }
if FieldByName('PasswordLifespan').IsNull then
PasswordExpired := False
else
PasswordExpired :=
Date - FieldByName('DateLastPasswordChange').AsDateTime >=
FieldByName('PasswordLifespan').AsInteger;
finally
Close;
end;
end;
end;
procedure TLoginDM.PostAuditTrail(EventID: Integer; EventMsg: String);
{ Writes an entry in the audit trail }
begin
with qryPostAuditTrail do
begin
ParamByName('ApplicationID').AsInteger := FLogin.ApplicationID;
if FLogin.UserID = cNoUserID then { This would happen when posting a bad login event }
ParamByName('UserID').Clear { We want a null userID in this case }
else
ParamByName('UserID').AsInteger := FLogin.UserID;
ParamByName('EventID').AsInteger := EventID;
ParamByName('Description').AsString := EventMsg;
ExecSQL;
end;
end;
procedure TLoginDM.PostUserLoginDate;
{ Writes the current date as the user's "date last logged in" }
begin
with qrySetLastLoggedInDate do
begin
ParamByName('UserID').AsInteger := FLogin.UserID;
ExecSQL;
end;
end;
end.